<?php

namespace app\api\controller;

use app\api\controller\Tpmecms;
use think\Db;
use think\Config;
/**
 * 公共接口
 */
class Com extends Tpmecms
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 获取空间背景图列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @return void
     */
    public function get_zoneimage_list($page=1,$limit=10)
    {
        $list = Db::name('zone_image')->page($page,$limit)->select();
        $this->success('ok',$list);
    }


    /**
     * 获取表情图列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @return void
     */
    public function get_exp_list($page=1,$limit=10)
    {
        $list = Db::name('emoji')->page($page,$limit)->select();
        $this->success('ok',$list);
    }

   


    /**
     * 个性列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @param int $sex 1男2女
     * @return void
     */
    public function get_character_list($page=1,$limit=10,$sex=null)
    {
        $where['status'] = '1';
        if($sex){
            $where['sex'] = $sex;
        }
        $list = Db::name('character')->where($where)->page($page,$limit)->field('id,name')->select();
        $this->success('ok',$list);
    }

    /**
     * 添加个性标签
     *
     * @param string $name 个性名
     * @return void
     */
    public function add_character($name)
    {
        if($this->auth->sex<1)$this->error('请先设置性别');
        $val = Db::name('character')
                ->where('name',$name)
                ->find();
        unset($val['uid']);
        if(!$val){
            $val['name'] = $name;
            $val['createtime'] = time();
            $val['status'] = '2';
            $val['sex'] = $this->auth->sex;
            $val['uid'] = $this->auth->id;
            $val['id'] = Db::name('character')
                ->insertGetId($val);

        }
        $this->success('ok',$val);
    }


    /**
     * 获取学校列表
     *
     * @param string $name 搜索(如果没有会自动添加)
     * @param integer $page 1
     * @param integer $limit 10
     * @return void
     */
    public function get_school_list($name=null,$page=1,$limit=10)
    {
        
        if($name)
        {
            $list = Db::name('school')
                ->where('name','like',"%".$name."%")
                ->page($page,$limit)
                ->select();
            if(sizeof($list)==0){
                Db::name('school')->insert(['name'=>$name,'status'=>'2','createtime'=>time()]);
                $list = Db::name('school')
                    ->where('name','like',"%".$name."%")
                    ->page($page,$limit)
                    ->select();
            }
            
        }else{
            $list = Db::name('school')
                ->where('status','1')//状态:1=公共,2=个人
                ->page($page,$limit)
                ->select();
        }
        
        $this->success('ok',$list);
    }


    /**
     * 获取配置
     *
     * @param string $field 获取需要的字段例如name（可不传获取全部
     * @return void
     */
    public function get_config($field = null)
    {
        if ($field) {
            $this->success($field, Config::get('site.' . $field));
        } else {
            $this->success('all', Config::get('site'));
        }
    }



    public function get_subject_group()
    {
        // 获取今日话题
        
        //查询 不同的组
        $list = Db::name('chat')->distinct(true)->field('group')->column('group');
        // halt($list);
        $list = Db::name('chat')->where('group','in',$list)->select();

        $user_arr = [];
        //合并用户
        foreach($list as $chat){
            // halt($chat);
            $user_arr[] = $chat['re_uid'];
            $user_arr[] = $chat['send_uid'];
        }
        //去重
        $user_arr = array_unique($user_arr);
        
        // 查询没有关闭的用户
        $user_ids = Db::name('user')->where('id','in',$user_arr)->where('subject_status','2')->column('id');
        // halt($user_ids);
        $group = [];
        foreach ($list as $key => $chat) {
       
            if(in_array($chat['re_uid'],$user_ids) && in_array($chat['send_uid'],$user_ids)){
                if(!in_array($chat['group'],$group))
                    $group[] = $chat['group'];
            }
        }

        $this->success('ok',implode(',',$group));
        //卸载关闭话题的组
    }
}
